package com.appleframework.module.usermgr.dao;

import java.sql.Types;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.appleframework.core.BaseDao;
import com.appleframework.module.usermgr.vo.Item;
import com.appleframework.module.usermgr.vo.Itemdetail;
import com.appleframework.module.usermgr.vo.SUser;

@Repository("itemDetailMgrDao")
public class ItemDetailMgrDao extends BaseDao {
	
	
    
	
	//根据字段查询
	public List<Itemdetail>  getItemdetailsByColunm(String columnName, Object value) {
		
		return getJdbcTemplate().query("select *  from t_item_detail where "+columnName+"=?", new BeanPropertyRowMapper(Itemdetail.class),value);
	}
	//查询所有
	public List<Itemdetail>  getItemdetails() {
		System.out.println("查询所有");
		return getJdbcTemplate().query("select *  from t_item_detail", new BeanPropertyRowMapper(Itemdetail.class));
	}
	
	/**
	 * 添加
	 * @param itemdetail
	 */
	public void addItemdetail(Itemdetail itemdetail) {
		String sql="insert into t_item_detail (name,descript,price,discount,parentid,file_path,createtime,createuserid)"
			+" values('"+itemdetail.getName()+"','"+itemdetail.getDescript()+"',"+itemdetail.getPrice()+","+itemdetail.getDiscount()+","+itemdetail.getParentid()+",'"+itemdetail.getFilepath()+"',now(),"+itemdetail.getCreateuserid()+")";
		System.out.println(sql);
		
		getJdbcTemplate().update(sql);
	}
	
	//删除
	public void deleteItemDetail(String columnName, Object value){
		String sql="delete from t_item_detail where "+columnName+"="+value;
		System.out.println(sql);
		getJdbcTemplate().update(sql);
	
	}
	
	//修改
	public void updateItem(Itemdetail itemdetail){
		String sql="update t_item_detail set name='"+itemdetail.getName()+"',descript='"+itemdetail.getDescript()+"',price="+itemdetail.getPrice()+",discount="+itemdetail.getDiscount()+",parentid="+itemdetail.getParentid()+",file_path='"+itemdetail.getFilepath()+"',updatetime=now() where id="+itemdetail.getId();
		System.out.println(sql);
		getJdbcTemplate().update(sql);
		
		
	}
	/**
	 * 分页显示信息  --根据name/parentid/所有，进行分页显示
	 * @param start 记录开始
	 * @param pageSize 每页显示数
	 * @param name 名称
	 * @param parentid 父id（菜品类别）
	 * @return
	 */
	public List<Itemdetail> getPage(Integer start, Integer pageSize,String name,Integer parentid){
		System.out.println(parentid);
		/**
		 * 分页显示信息  --根据name/id/所有，进行分页显示
		 * @param  pageNow当前第几页
		 * @param  pageSize每一页显示的条数
		 * @return 当前第几页的pageSize条记录
		 */
		//String sql="select * from t_item ";
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_item_detail where 1 = 1");
		
		if(null != name && !name.equals("")){
			sb.append(" and name like '%");
			sb.append(name);
			sb.append("%'");
		}if(-1 != parentid){
			sb.append(" and parentid = ");
			sb.append(parentid);
		}
		
		sb.append(" limit ");
		sb.append(start);
		sb.append(",");
		sb.append(pageSize);
		
		System.out.println(sb.toString());
		return getJdbcTemplate().query(sb.toString(),BeanPropertyRowMapper.newInstance(Itemdetail.class) );
		
		
		
	}
	
	
	/**
	 *  按条件查询一共多少条记录  
	 * 
	 * @return 
	 */
	public int findRecordCount(String name,Integer parentid){
		
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) from t_item_detail where 1=1");
		
		if(null != name && !name.equals("")){
			sb.append(" and name like '%");
			sb.append(name);
			sb.append("%'");
		}if(-1 != parentid){
			sb.append(" and parentid = ");
			sb.append(parentid);
		}
		return getJdbcTemplate().queryForInt(sb.toString());
	};
	

	

}
